package com.gxljc.bear.crawler.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;

import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;

public class XmlToMapJsonUtil {
	/**
	 * 将xml转换为json格式
	 * 
	 * @param xml
	 * @return
	 * @author haitao
	 */
	public static String xml2JSON(String xml) {
		return new XMLSerializer().read(xml).toString();
	}

	@SuppressWarnings({ "unused", "rawtypes" })
	public static Map json2XML(String json) {
		JSONObject jobj = JSONObject.fromObject(json);
		Map map = jobj;
		String xml = new XMLSerializer().write(jobj);
		return map;
	}

	/**
	 * 将xml页面元素转换成map
	 * 
	 * @param map
	 * @param ele
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	static void ele2map(Map map, Element ele) {
		List<Element> elements = ele.elements();
		if (elements.size() == 0) {
			map.put(ele.getName(), ele.getText());
		} else if (elements.size() == 1) {
			Map<String, Object> tempMap = new HashMap<String, Object>();
			ele2map(tempMap, elements.get(0));
			map.put(ele.getName(), tempMap);
		} else {
			// 多个子节点的话就得考虑list的情况了，比如多个子节点有节点名称相同的
			// 构造一个map用来去重
			Map<String, Object> tempMap = new HashMap<String, Object>();
			for (Element element : elements) {
				tempMap.put(element.getName(), null);
			}
			Set<String> keySet = tempMap.keySet();
			for (String string : keySet) {
				Namespace namespace = elements.get(0).getNamespace();
				List<Element> elements2 = ele.elements(new QName(string, namespace));
				// 如果同名的数目大于1则表示要构建list
				if (elements2.size() > 1) {
					List<Map> list = new ArrayList<Map>();
					for (Element element : elements2) {
						Map<String, Object> tempMap1 = new HashMap<String, Object>();
						ele2map(tempMap1, element);
						list.add(tempMap1);
					}
					map.put(string, list);
				} else {
					// 同名的数量不大于1则直接递归去
					Map<String, Object> tempMap1 = new HashMap<String, Object>();
					ele2map(tempMap1, elements2.get(0));
					map.put(string, tempMap1);
				}
			}
		}
	}

	@SuppressWarnings("rawtypes")
	public static Map xml2map(String xmlString) throws DocumentException {
		Document doc = DocumentHelper.parseText(xmlString);
		Element rootElement = doc.getRootElement();
		Map<String, Object> map = new HashMap<String, Object>();
		ele2map(map, rootElement);
		// 转json判断xml2map转换的是否ok
		// String string = JSONObject.fromObject(map).toString();
		return map;
	}

}
